package com.zzyl.serve.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.service.INursingProjectService;

/**
 * 护理项目Service业务层处理
 *
 * @author itheima
 * @date 2025-02-28
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    //redis KEY的常量
    private static final String KEY_NURSING_PROJECT_ALL="nursingProject:all";

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Integer id) {
//        return nursingProjectMapper.selectNursingProjectById(id);
        return getById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
//        return nursingProjectMapper.selectNursingProjectList(nursingProject);
        return list(
                new LambdaQueryWrapper<NursingProject>()
                        .like(nursingProject.getName() != null, NursingProject::getName, nursingProject.getName())
                        .eq(nursingProject.getStatus() != null, NursingProject::getStatus, nursingProject.getStatus())
        );
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        nursingProject.setCreateTime(DateUtils.getNowDate());
//        return nursingProjectMapper.insertNursingProject(nursingProject);
        // 删除缓存
        deleteCache();
        return save(nursingProject) ? 1 : 0;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
//        nursingProject.setUpdateTime(DateUtils.getNowDate());
//        return nursingProjectMapper.updateNursingProject(nursingProject);
        // 删除缓存
        deleteCache();
        return updateById(nursingProject) ? 1 : 0;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Integer[] ids) {
//        return nursingProjectMapper.deleteNursingProjectByIds(ids);
        // 删除缓存
        deleteCache();
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Integer id) {
//        return nursingProjectMapper.deleteNursingProjectById(id);
        // 删除缓存
        deleteCache();
        return removeById(id) ? 1 : 0;
    }


    /**
     * 查询所有 护理项目
     * @return
     */
    @Override
    public List<NursingProjectVo> selectAll() {
        //1.先查询缓存
        List<NursingProjectVo> list = (List<NursingProjectVo>) redisTemplate.opsForValue().get(KEY_NURSING_PROJECT_ALL);
        //2.断缓存是否存在
        if(list!=null && list.size()>0){
            //如果存在直接返回
            return list;
        }

        List<NursingProjectVo> projectList = nursingProjectMapper.selectAll();
        //3.不存在，查询数据库，并且存入缓存
        redisTemplate.opsForValue().set(KEY_NURSING_PROJECT_ALL, projectList);
        return projectList;

    }

    private void deleteCache() {
        // 删除缓存
        redisTemplate.delete(KEY_NURSING_PROJECT_ALL);
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @Override
    public List<NursingProject> selectPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        LambdaQueryWrapper<NursingProject> eq = new LambdaQueryWrapper<NursingProject>()
                .like(name != null, NursingProject::getName, name)
                .eq(status != null, NursingProject::getStatus, status);

        List<NursingProject> nursingProjects = nursingProjectMapper.selectList(eq);
        return nursingProjects;
    }

    /**
     * 分页查询护理项目
     * @return
     */
    /*@Override
    public TableDataInfo selectPage(Integer pageNum, Integer pageSize) {

        //分页查询数据
        IPage page = new Page(pageNum,pageSize);

        page = page(page, null);
        return getTableDataInfo(page);
    }

    *//**
     * 封装分页信息
     * @param page
     * @return
     *//*
    private TableDataInfo getTableDataInfo(IPage page) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("请求成功");
        rspData.setRows(page.getRecords());
        rspData.setTotal(page.getTotal());
        return rspData;
    }*/
}
